

#import "BIDCommitDetailViewController.h"
#import "BIDAppDelegate.h"

@interface BIDCommitDetailViewController ()

@end

@implementation BIDCommitDetailViewController{
    NSManagedObject *commit;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //NSLog(@"x %f, y %f, w %f, h %f", self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
    [self.tableView registerClass:[UITableViewCell class]
      forCellReuseIdentifier:@"plainCell"];
    self.textView = [[UITextView alloc] init];
    self.textView.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)/3);
    self.textView.editable = FALSE;
    self.textView.font = [UIFont boldSystemFontOfSize:16];
    self.textView.autoresizesSubviews = YES;
    self.textView.text = @"This is the table header view";
    self.tableView.tableHeaderView = self.textView;
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //fetch all commits from core date with name = table.title
    BIDAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Commit"];
    NSError *error;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:
                              @"(revision = %@)",[NSNumber numberWithInt:[[self.title componentsSeparatedByString:@" "][1] intValue]]];
    [request setPredicate:predicate];
    NSArray *objects = [context executeFetchRequest:request error:&error];
    if (objects != nil && [objects count] != 0){
        //NSLog(@"dev name: %@", [objects[0] valueForKey:@"developer"]);
        commit = objects[0];
        self.textView.text = [[objects[0] valueForKey:@"commitComment"] valueForKey:@"comment"];
    }
    [self.tableView reloadData];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [[commit valueForKey:@"commitFiles"] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //NSLog(@"tableView x: %f, y: %f", tableView.frame.origin.x, tableView.frame.origin.y);
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"plainCell" forIndexPath:indexPath];
    cell.textLabel.font = [UIFont systemFontOfSize:10];
    cell.textLabel.textAlignment = NSTextAlignmentLeft;
    cell.textLabel.lineBreakMode = NSLineBreakByCharWrapping;
    cell.textLabel.text = [[[commit valueForKey:@"commitFiles"] allObjects][indexPath.row] valueForKey:@"filePath"];
    // Configure the cell...
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

@end
